<?php session_start();

if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{
	if(isset($_SESSION['type']))
	{
		if($_SESSION['type'] == 'administrator')
		{
			header('Location: admin_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'student')
		{
			header('Location: student_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'tutor')
		{
			header('Location: tutor_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'parent')
		{
			$host = 'localhost';
			$dbuser = 'postgres';
			$dbpass = 'password';
			$dbname = $_SESSION['database'];
			$username = $_SESSION['user'];
				
			pg_connect("host=$host user=$dbuser password=$dbpass dbname=$dbname") or die(pg_last_error());
				
			?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reservations</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<style>
.nav {
	width: 210px;
	height: auto;
	margin-left: 10px;
	text-align: center;
}

.mainline {
	width: 712px;
	height: auto;
	text-align: center;
}
</style>
</head>

<body bgcolor="#FFFFFF">
	<center>
		<div id="wrapper">
			<div id="header">
				<h1></h1>
			</div>

			<div id="header2">
				<h3></h3>
			</div>

			<div id="nav">
			<?php include 'parent_navigation.php';?>
			</div>

			<div id="mainline">
				<h4>View Reservations</h4>
			</div>

			<div id="body">
			<?php
			if(isset($_SESSION['message']) && $_SESSION['message'] != ""){
				$message = $_SESSION['message'];
				echo "<i>$message<i><br>";
				unset($_SESSION['message']);
			}
			?>

				<center>
					<table width="700" height="auto" cellpadding="3" cellspacing="1">
						<tr bgcolor="LightSteelBlue">
							<th width="100">Subject</th>
							<th width="150">Schedule</th>
							<th width="100">Room</th>
							<th width="150">Instructor</th>
							<th width="100">Start/End Date</th>
							<th width="100">Fee</th>
							<th width="50">Action</th>
						</tr>
						<?php
						$sql = "SELECT * FROM reservations NATURAL INNER JOIN classes WHERE username = '$username'";			//from reserved classes
						$result = pg_query($sql);


						$count = pg_num_rows($result);

						if ($count < 1){
							echo "</table><br />You have no reservations.";
						}
						for($i = 0; $i < $count; $i++)
						{
							$subject = pg_fetch_result($result, $i, 'subject');
							$section = pg_fetch_result($result, $i, 'section');
							$startdate = pg_fetch_result($result, $i, 'startdate');
							$enddate = pg_fetch_result($result, $i, 'enddate');
							//$fee = pg_fetch_result($result, $i, 'fee');
							$starttime= pg_fetch_result($result, $i, 'starttime');
							$endtime= pg_fetch_result($result, $i, 'endtime');
							$instructor= pg_fetch_result($result, $i, 'teacher_id');
							$sql2 = "SELECT lastname, firstname from users natural inner join teachers where teacher_id = '$instructor'";
							$result2 = pg_query($sql2);
							$lastname = pg_fetch_result($result2, 0, 'lastname');
							$firstname = pg_fetch_result($result2, 0, 'firstname');
							$name = $firstname . " " . $lastname;
							$room= pg_fetch_result($result, $i, 'room_id');
							$sql2 = "SELECT * FROM rooms natural inner join buildings where room_id = '$room'";
							$result2 = pg_query($sql2);
							$room_number = pg_fetch_result($result2, 0, 'room_number');
							$building_name = pg_fetch_result($result2, 0, 'building_name');
							$room = $building_name . " " . $room_number;
							$reservation_id = pg_fetch_result($result, $i, 'reservation_id');
							//$building= pg_fetch_result($result, $i, 'building');

							echo "
								<form action=\"parent_cancelreserve.php\" method=\"post\">
									<tr height=\"30\" bgcolor=\"#f5f5f5\">
										<td>$subject<br />$section</td>
										<td>$starttime<br />$endtime</td>
										<td>$room</td>
										<td>$name</td>
										<td>$startdate<br />$enddate</td>
										<td />
										<input type=\"hidden\" name=\"reservation_id\" value=$reservation_id />
										<td><input type=\"submit\" name=\"action\" value=\"Cancel\" /></td>
									</tr>
								</form>";						
						}
						?>
					</table>
				</center>

			</div>

			<p align="justify">
				<br />
			</p>
		

		<br class="clearfloat"> <br />

		<div id="footer">
			<p align="center">
				Tutorial Business Manager: info@tbms.com <br />Credits/Engineers:
				Aquino, Chloebelle; Dela Cruz, Roy Erwin; Maliwanag, Ranyel Bryan;
				Manzano, Audrey Ann; Ng, Sheila Regine<br /> <br />copyright 2011
			</p>
		</div>
		</div>
	</center>
</body>

						<?php
		}
	}
}
else
{
	header('Location: index.php');
}
?>